<template>
	<view style="width: 100%; height: 100%;">
		<web-view style="width: 100%; height: 100%;" :fullscreen="false" :src="trueUrl" @message="handlePostMessage"></web-view>
	</view>
</template>

<script>
	export default {
		name:"bg-pdf",
		props:["title", "propUrl"],
		data() {
			return {
				totalPage:1,
				currentReadPage:1,
				trueUrl:'',
				viewerUrl: '/hybrid/html/web/viewer.html',
				fileUrl:'https://www.nasa.gov/wp-content/uploads/static/history/SP-4225/search.pdf'
			};
		},
		mounted() {
			// #ifdef H5
			window.addEventListener("message", this.ReceiveMessage);
			// #endif
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: this.title
			});
			/* 初始页面 */
			this.pageInt(); //获取pdfs数据
		},
		
		methods:{
			pageInt() {
				this.trueUrl = `${this.viewerUrl}?file=${encodeURIComponent(this.fileUrl)}&page=` + 1;
			},
			/* 
			 *	做成监听滚动条判断更好
			 * 
			 */
			//uni 组件通信 监听
			handlePostMessage(data) {
				let arr = data.detail.data.pop()
				this.totalPage = arr[0].totalPage //总页数
				this.currentReadPage = arr[1].page + 1 //当前页数
				console.log("app:", this.totalPage, this.currentReadPage);
			},
			//h5 监听
			ReceiveMessage(event) {
				if (event.data && event.data.data && event.data.data.arg) {
					this.totalPage = event.data.data.arg[0].totalPage
					this.currentReadPage = event.data.data.arg[1].page + 1
				}
				console.log("app:", this.totalPage, this.currentReadPage);
			},
			
			//页面销毁前动作
			addBrowseRecord() {
				// console.log("总页数：",this.totalPage);
				// console.log("当前页数：",this.currentReadPage);
			},
		}
	}
</script>

<style>

</style>